package com.google.code.xconf.utils;

public class NumberTools {

    public static Long longValue(Object object) {
        if (object instanceof Integer) {
            return ((Integer) object).longValue();
        } else if (object instanceof Double) {
            return ((Double) object).longValue();
        } else if (object instanceof Float) {
            return ((Float) object).longValue();
        } else if (object instanceof Short) {
            return ((Short) object).longValue();
        } else if (object instanceof Long) {
            return (Long) object;
        } else if (object instanceof String) {
            try {
                return Long.valueOf((String) object);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static Integer intValue(Object object) {
        if (object instanceof Integer) {
            return ((Integer) object);
        } else if (object instanceof Double) {
            return ((Double) object).intValue();
        } else if (object instanceof Float) {
            return ((Float) object).intValue();
        } else if (object instanceof Short) {
            return ((Short) object).intValue();
        } else if (object instanceof Long) {
            return ((Long) object).intValue();
        } else if (object instanceof String) {
            try {
                return Integer.valueOf((String) object);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static Double doubleValue(Object object) {
        if (object instanceof Integer) {
            return ((Integer) object).doubleValue();
        } else if (object instanceof Float) {
            return ((Float) object).doubleValue();
        } else if (object instanceof Short) {
            return ((Short) object).doubleValue();
        } else if (object instanceof Long) {
            return ((Long) object).doubleValue();
        } else if (object instanceof Double) {
            return (Double) object;
        } else if (object instanceof String) {
            try {
                return Double.valueOf((String) object);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

}
